<?php
/* SVN FILE: $Id: marketFormatter.php 248 2009-11-12 16:29:29Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* ibMarket Formatter
* Formats any text or number
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 248 $
* @lastrevision $Date: 2009-11-12 16:29:29 +0000 (Thu, 12 Nov 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-11-12 16:29:29 +0000 (Thu, 12 Nov 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/sources/marketFormatter.php $
*/

final class marketFormatter
{
	/**
	 * Sets when the class is initiated
	 *
	 * @access	private
	 * @var		boolean
	 */
	private static $_is_init = false;
	
	/**
	 * Turn off constructor
	 *
	 * @access	private
	 * @return	void
	 */
	private function __construct() {}
	
	/**
	 * Sets up this class. To be called first and only once.
	 *
	 * @access	private
	 * @return	void
	 */
	public static function __init()
	{
		if(self::$_is_init)
		{
			return;
		}
		
		self::$_is_init = true;
	}
	
	/**
	 * Formats a number based on localized data with an option to drop useless zeroes
	 *
	 * @access	public
	 * @param	mixed	$number				Number to format
	 * @param	integer	[$decimalPlaces]	Decimal places
	 * @param	bool	[$minimal]			Determines to remove useless zeroes
	 * @return	string						Formatted number
	 */
	public static function numberFormat($number, $decimalPlaces = 0, $minimal = true)
	{
		if($minimal && $decimalPlaces > 0)
		{
			while($decimalPlaces > 0 && numbers::round($number,$decimalPlaces - 1) == $number)
			{
				$decimalPlaces--;
			}
		}
		return ipsRegistry::getClass('class_localization')->formatNumber($number, $decimalPlaces);
	}
	
	/**
	 * Formats a number into ibMarket currency format
	 *
	 * @access	public
	 * @param	mixed	$amount				Amount of money
	 * @param	integer	[$decimalPlaces]	Decimal places
	 * @param	bool	[$minimal]			Determines to remove useless zeroes
	 * @return	string						Formatted currency number
	 */
	public static function formatCurrency($amount, $decimalPlaces = 0, $minimal = true)
	{
		if(numbers::compare($amount) < 0)
		{
			$_number = self::numberFormat($amount,$decimalPlaces,$minimal);
			return "-".self::currencyFormat(substr($_number,1));
		}
		else
		{
			return self::currencyFormat(self::numberFormat($amount,$decimalPlaces,$minimal));
		}
	}
	
	/**
	 * Formats anything into ibMarket currency format
	 *
	 * @access	public
	 * @param	mixed	$text	Text to format
	 * @return	string	Formatted currency number
	 */
	public static function currencyFormat($text)
	{
		$settings =& ipsRegistry::fetchSettings();
		return $settings['market_prefix'].$text.$settings['market_suffix'];
	}
	
	/**
	 * Extends the relative time provided by IP.Board to include times in the future
	 *
	 * @access	public
	 * @param	integer	$relativeTime	Time to compare to
	 * @param	integer	[$currentTime]	Time relative to
	 * @return	string					Time relative to $currentTime
	 */
	public static function timeRelative($relativeTime, $currentTime = IPS_UNIX_TIME_NOW)
	{
		$difference = $relativeTime - $currentTime;
		
		$return = "";
		$settings =& ipsRegistry::settings();
		if($difference < 0 || !$settings['time_use_relative'])
		{
			$return = ipsRegistry::getClass('class_localization')->getDate($relativeTime,'LONG',0,1);
		}
		elseif($difference == 0)
		{
			$return = $this->ipsclass->lang['now'];
		}
		elseif($difference == 1)
		{
			$return = sprintf(ipsRegistry::getClass('class_localization')->words['in_time'], ipsRegistry::getClass('class_localization')->words['1_second']);
		}
		elseif($difference <= 60)
		{
			$return = sprintf(ipsRegistry::getClass('class_localization')->words['in_time'], sprintf(ipsRegistry::getClass('class_localization')->words['x_seconds'], $difference));
		}
		elseif($difference <= 120)
		{
			$return = sprintf(ipsRegistry::getClass('class_localization')->words['in_time'], ipsRegistry::getClass('class_localization')->words['1_minute']);
		}
		elseif($difference <= 3600)
		{
			$return = sprintf(ipsRegistry::getClass('class_localization')->words['in_time'], sprintf(ipsRegistry::getClass('class_localization')->words['x_minutes'], floor($difference / 60)));
		}
		elseif($difference <= 7200)
		{
			$return = sprintf(ipsRegistry::getClass('class_localization')->words['in_time'], ipsRegistry::getClass('class_localization')->words['1_hour']);
		}
		elseif($difference <= 86400)
		{
			$return = sprintf(ipsRegistry::getClass('class_localization')->words['in_time'], sprintf(ipsRegistry::getClass('class_localization')->words['x_hours'], floor($difference / 3600)));
		}
		elseif($difference <= 172800)
		{
			$return = sprintf(ipsRegistry::getClass('class_localization')->words['in_time'], ipsRegistry::getClass('class_localization')->words['1_day']);
		}
		elseif($difference <= 604800)
		{
			$return = sprintf(ipsRegistry::getClass('class_localization')->words['in_time'],sprintf(ipsRegistry::getClass('class_localization')->words['x_days'], floor($difference / 86400)));
		}
		elseif($difference <= 1209600)
		{
			$return = sprintf(ipsRegistry::getClass('class_localization')->words['in_time'], ipsRegistry::getClass('class_localization')->words['1_week']);
		}
		else // In many many weeks ahead
		{
			$return = sprintf(ipsRegistry::getClass('class_localization')->words['in_time'], sprintf(ipsRegistry::getClass('class_localization')->words['x_weeks'], floor($difference / 604800)));
		}
		return $return;
	}
	
	/**
	 * Gets the words for relative amount of time, not from current time. This will use largest relavent time unit. This will disreguard any smaller units.
	 * Ex. if $relativeTime is more than hours but less than a day, it will use hours.
	 *
	 * @access	public
	 * @param	integer	$relativeTime	The time in seconds, not Unix time, must be positive
	 * @return	string
	 */
	public static function relativeTimeWord($relativeTime)
	{
		// The times returned are approximates
		if($relativeTime == 1)
		{
			$return = ipsRegistry::getClass('class_localization')->words['1_second'];
		}
		elseif($relativeTime < 60)
		{
			$return = sprintf(ipsRegistry::getClass('class_localization')->words['x_seconds'],$relativeTime);
		}
		elseif($relativeTime < 120)
		{
			$return = ipsRegistry::getClass('class_localization')->words['1_minute'];
		}
		elseif($relativeTime < 3600)
		{
			$return = sprintf(ipsRegistry::getClass('class_localization')->words['x_minutes'], floor($relativeTime / 60));
		}
		elseif($relativeTime < 7200)
		{
			$return = ipsRegistry::getClass('class_localization')->words['1_hour'];
		}
		elseif($relativeTime < 86400)
		{
			$return = sprintf(ipsRegistry::getClass('class_localization')->words['x_hours'], floor($relativeTime / 3600));
		}
		elseif($relativeTime < 172800)
		{
			$return = ipsRegistry::getClass('class_localization')->words['1_day'];
		}
		elseif($relativeTime < 604800)
		{
			$return = sprintf(ipsRegistry::getClass('class_localization')->words['x_days'], floor($relativeTime / 86400));
		}
		elseif($relativeTime < 1209600)
		{
			$return = ipsRegistry::getClass('class_localization')->words['1_week'];
		}
		else // In many many weeks ahead
		{
			$return = sprintf(ipsRegistry::getClass('class_localization')->words['x_weeks'], floor($relativeTime / 604800));
		}
		return $return;
	}

	/**
	 * Gets the words for relative amount of time, not from current time. This will use largest exact time unit.
	 * Ex. if $relativeTime is more than hours but less than a day, it will either use hours, minutes, or seconds. It will use hours if it is exactly on the hour, not a second more or less.
	 *
	 * @access	public
	 * @param	integer	$relativeTime	The time in seconds, not Unix time, must be positive
	 * @return	string
	 */
	public static function relativeTimeExact($relativeTime)
	{
		// The times returned are exact
		// Becare on using this because it may return seconds
		if($relativeTime % 604800 == 0)
		{
			if($relativeTime == 604800)
			{
				$return = sprintf(ipsRegistry::getClass('class_localization')->words['x_days'], 7);
			}
			else
			{
				$return = sprintf(ipsRegistry::getClass('class_localization')->words['x_weeks'], floor($relativeTime / 604800));
			}
		}
		elseif($relativeTime % 86400 == 0)
		{
			if($relativeTime == 86400)
			{
				$return = sprintf(ipsRegistry::getClass('class_localization')->words['x_hours'],24);
			}
			else
			{
				$return = sprintf(ipsRegistry::getClass('class_localization')->words['x_days'], floor($relativeTime / 604800));
			}
		}
		elseif($relativeTime % 3600 == 0)
		{
			if($relativeTime == 3600)
			{
				$return = sprintf(ipsRegistry::getClass('class_localization')->words['x_minutes'], 60);
			}
			else
			{
				$return = sprintf(ipsRegistry::getClass('class_localization')->words['x_hours'], floor($relativeTime / 3600));
			}
		}
		elseif($relativeTime % 60 == 0)
		{
			if($relativeTime == 60)
			{
				$return = sprintf(ipsRegistry::getClass('class_localization')->words['x_seconds'], 60);
			}
			else
			{
				$return = sprintf(ipsRegistry::getClass('class_localization')->words['x_minutes'], floor($relativeTime / 60));
			}
		}
		else //if($total_time % 1 == 0)
		{
			if($relativeTime == 1)
			{
				$return = ipsRegistry::getClass('class_localization')->words['1_second'];
			}
			else
			{
				$return = sprintf(ipsRegistry::getClass('class_localization')->words['x_seconds'], $relativeTime);
			}
		}
		
		return $return;
	}
	
	/**
	 * Gets the words for relative amount of time, not from current time. This will use largest exact time unit.
	 * Ex. if $relativeTime is more than hours but less than a day, it will use hours, minutes, and seconds.
	 *
	 * @access	public
	 * @param	integer	$relativeTime	The time in seconds, not Unix time, must be positive
	 * @param	string	$method			What type relative to use, same as IPB's getDate $method
	 * @return	string
	 */
	public static function relativeTimeFull($relativeTime, $method = 'SHORT')
	{
		$method = strtoupper($method);
		$rtn = array();
		if($relativeTime >= 604800)
		{
			if($method != 'LONG')
			{
				$rtn[] = floor($relativeTime/604800)."w";
			}
			else
			{
				if($time >= 1209600)
				{
					$rtn[] = sprintf(ipsRegistry::getClass('class_localization')->words['x_weeks'], floor($time / 604800));
				}
				else
				{
					$rtn[] = ipsRegistry::getClass('class_localization')->words['1_week'];
				}
			}
			$relativeTime = $relativeTime % 604800;
		}
		if($relativeTime >= 86400)
		{
			if($method != 'LONG')
			{
				$rtn[] = floor($relativeTime/86400)."d";
			}
			else
			{
				if($relativeTime >= 172800)
				{
					$rtn[] = sprintf(ipsRegistry::getClass('class_localization')->words['x_days'], floor($time / 86400));
				}
				else
				{
					$rtn[] = ipsRegistry::getClass('class_localization')->words['1_day'];
				}
			}
			$relativeTime = $relativeTime % 86400;
		}
		if($relativeTime >= 3600)
		{
			if($method != 'LONG')
			{
				$rtn[] = floor($relativeTime/3600)."h";
			}
			else
			{
				if($relativeTime >= 7200)
				{
					$rtn[] = sprintf(ipsRegistry::getClass('class_localization')->words['x_hours'], floor($relativeTime / 3600));
				}
				else
				{
					$rtn[] = ipsRegistry::getClass('class_localization')->words['1_hour'];
				}
			}
			$relativeTime = $relativeTime % 3600;
		}
		if($relativeTime >= 60)
		{
			if($method != 'LONG')
			{
				$rtn[] = floor($relativeTime/60)."m";
			}
			else
			{
				if($relativeTime >= 120)
				{
					$rtn[] = sprintf(ipsRegistry::getClass('class_localization')->words['x_minutes'], floor($relativeTime / 60));
				}
				else
				{
					$rtn[] = ipsRegistry::getClass('class_localization')->words['1_minute'];
				}
			}
			$relativeTime = $relativeTime % 60;
		}
		if($relativeTime && count($rtn) <= 1)
		{
			if($method != 'LONG')
			{
				$rtn[] = $relativeTime."s";
			}
			else
			{
				if($left >= 2)
				{
					$rtn[] = sprintf(ipsRegistry::getClass('class_localization')->words['x_seconds'],$relativeTime);
				}
				else
				{
					$rtn[] = ipsRegistry::getClass('class_localization')->words['1_second'];
				}
			}
			$time = 0;
		}
		if(!$rtn)
		{
			if($method != 'LONG')
			{
				$rtn[] = "0s";
			}
			else
			{
				$rtn[] = sprintf(ipsRegistry::getClass('class_localization')->words['x_seconds'],0);
			}
		}
		
		if($method != 'LONG')
		{
			return implode(" ", $rtn);
		}
		else
		{
			return implode(", ", $rtn);
		}
	}
	
	/**
	 * Gets the words for relative amount of time, not from current time. This will use largest exact time unit.
	 *
	 * @access	public
	 * @param	integer	$relativeTime	The time in seconds, not Unix time, must be positive
	 * @return	string
	 */
	public static function timeDifference($time = 0,$type = 'LONG',$norelative = 0)
	{
		$settings =& ipsRegistry::settings();$settings =& ipsRegistry::settings();
		if($time < IPS_UNIX_TIME_NOW || $norelative || !$settings['time_use_relative'])
		{
			return ipsRegistry::getClass('class_localization')->getDate($time,$type,$norelative);
		}
		
		return self::relativeTimeFull($time - IPS_UNIX_TIME_NOW,$type);
	}
	
	/**
	 * The member name with the link
	 *
	 * @access	public
	 * @param	array|int	$member	The member or member_id
	 * @return	string		The link to the member
	 */
	public static function formatName($member)
	{
		if($member)
		{
			if(is_int($member))
			{
				$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$member));
			}
			
			$member['_formatted_name'] = IPSLib::makeNameFormatted($member['members_display_name'],$member['member_group_id']);
			
			if(IN_ACP)
			{
				return $member['_formatted_name'];
			}
			else
			{
				return ipsRegistry::getClass('output')->getTemplate('ibmarket')->formatName($member);
			}
		}
		else
		{
			return isset(ipsRegistry::getClass('class_localization')->words['global_guestname']) ? ipsRegistry::getClass('class_localization')->words['global_guestname'] : "Guest";
		}
	}
	
}

marketFormatter::__init();
?>